package cn.com.bdqn.controller;

import cn.com.bdqn.entity.CstService;
import cn.com.bdqn.entity.SalChance;
import cn.com.bdqn.service.CstServiceService;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cstService")
@Api(tags = "客户服务管理接口")
public class CstServiceController {

    @Autowired
    private CstServiceService cstServiceService;

    @Operation(summary = "新增客户服务")
    @PostMapping("/addCstService")
    public ReturnData addCstService(CstService cstService) {
        int result = cstServiceService.addCstService(cstService);
        if (result > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.currError("新增客户服务异常");
        }
    }

    @Operation(summary = "指派客户服务")
    @PostMapping("/assignment")
    public ReturnData assignment(CstService cstService) {
        int result = cstServiceService.assignment(cstService);
        if (result > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.currError("指派客户服务异常");
        }
    }

    @Operation(summary = "服务管理高级查询")
    @PostMapping("/queryByLike")
    public ReturnData queryByLike(CstService cstService, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "15") int pageSize) {
        try {
            // 开始分页
            PageHelper.startPage(pageNum, pageSize);
            // 根据条件查询结果
            List<Map> mapList = cstServiceService.queryByLike(cstService);
            // 封装分页信息
            PageInfo pageInfo = new PageInfo(mapList, 3);
            // 返回成功请求，携带查询结果和分页信息
            return ReturnData.ok().put("mapList", mapList).put("pageInfo", pageInfo);
        } catch (Exception e) {
            // 出现异常时，返回错误信息
            return ReturnData.currError("数据库异常");
        }
    }

    @Operation(summary = "根据客户服务编号删除客户服务")
    @PostMapping("/delCstService")
    public ReturnData delCstService(Long svrId) {
        int result = cstServiceService.delCstService(svrId);
        if (result > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.currError("根据客户服务编号删除客户服务异常");
        }
    }

    @Operation(summary = "客户服务处理")
    @PostMapping("/dispose")
    public ReturnData dispose(CstService cstService) {
        int result = cstServiceService.dispose(cstService);
        if (result > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.currError("客户服务处理异常");
        }
    }

    @Operation(summary = "客户服务反馈")
    @PostMapping("/feedback")
    public ReturnData feedback(CstService cstService) {
        //  调用cstServiceService的feedback方法，并将返回值赋值给result1
        int  result1  =  cstServiceService.feedback(cstService);
        if  (result1  >  0){
            //  如果cstService的svrSatisfy小于3
            if  (cstService.getSvrSatisfy()  <  3){
                //  调用cstServiceService的anew方法，并将返回值赋值给result
                int  result  =  cstServiceService.anew(cstService);
                if  (result  >  0)  {
                    //  返回成功的ReturnData
                    return  ReturnData.ok();
                }  else  {
                    //  返回自定义错误信息的ReturnData
                    return  ReturnData.currError("客户服务归档异常");
                }
            }else  {
                //  返回成功的ReturnData
                return  ReturnData.ok();
            }
        }else  {
            //  返回自定义错误信息的ReturnData
            return  ReturnData.currError("客户服务反馈异常");
        }

    }

    @Operation(summary = "根据客户服务编号查询单个客户服务")
    @PostMapping("/queryBySvrId")
    public ReturnData queryBySvrId(Long svrId) {
        try {
            Map map = cstServiceService.queryBySvrId(svrId);
            return ReturnData.ok().put("map", map);
        } catch (Exception e) {
            return ReturnData.currError("根据客户服务编号查询单个客户服务异常");
        }
    }


}
